package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.Hutool;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;

import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author liyi
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String NURSINGPLAN_ALL = "nursingplan:all";
    private static final Integer STATUS_ENABLE = 1;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        // 查询护理计划
        NursingPlan nursingPlan = this.getById(id);
        // 查询项目计划中间表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        // 封装所有数据
        NursingPlanVo nursingPlanVo = BeanUtil.copyProperties(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.copyProperties(nursingProjectPlan, NursingProjectPlanVo.class);
//            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId().toString());
//            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        // 新增护理计划
        int row = nursingPlanMapper.insert(nursingPlan);
        // 新增项目计划中间表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        // 赋值每个中间表计划id
        projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        nursingProjectPlanService.saveBatch(projectPlans);

        // 清理缓存
        clearNursingPlanRedis();
        return row;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划接收请求参数
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        // 修改护理计划
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        boolean flag = this.updateById(nursingPlan);

        // 判断要修改的项目计划中间表是否为空
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        // 先删除关联的中项目计划中间表
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
        if (CollectionUtils.isNotEmpty(projectPlans)) {
            // 不为空新增项目计划中间表
            // 赋值每个中间表计划id
            projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
            nursingProjectPlanService.saveBatch(projectPlans);
        }

        // 清理缓存
        clearNursingPlanRedis();
        return flag ? 1 : 0;
    }

    /**
     * 删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Integer id) {
        // 删除绑定的项目计划中间表
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        // 删除护理计划
        boolean flag = this.removeById(id);

        // 清理缓存
        clearNursingPlanRedis();
        return flag ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id) {        // 清理缓存
        boolean flag = this.removeById(id);

        // 清理缓存
        clearNursingPlanRedis();
        return flag ? 1 : 0;
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> all() {
        List<NursingPlan> nursingPlanList = (List<NursingPlan>) redisTemplate.opsForValue().get(NURSINGPLAN_ALL);
        if (CollectionUtils.isEmpty(nursingPlanList)) {
            nursingPlanList = this.list(Wrappers.<NursingPlan>lambdaQuery().eq(NursingPlan::getStatus, STATUS_ENABLE));
            redisTemplate.opsForValue().set(NURSINGPLAN_ALL, nursingPlanList);
        }
        return nursingPlanList;
    }

    /**
     * 清理护理计划缓存
     */
    private void clearNursingPlanRedis() {
        redisTemplate.delete(NURSINGPLAN_ALL);
    }
}
